<?php

require_once("Database/Model/ExerciseDAL.php");
require_once("Database/Model/DatabaseConnection.php");

class WorkoutExercisesDAL 
{
	/**
    *@var ExerciseDAL
    **/
	private $exerciseDAL;
	 /**
    *@var LoginValidation 
    **/
	private $loginValidation;

	public function __construct()
	{
		$this->exerciseDAL = new ExerciseDAL();
		$this->loginValidation = new LoginValidation();
	}

	/**
    *@param int
    *@throws exception
    **/
	public function DeleteWorkoutExercise($workoutID)
	{
		  $sql = "DELETE FROM workoutexercises WHERE `WorkoutID` = $workoutID";  

		  $mysqli = new mysqli( DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

		  $stmt = $mysqli->prepare($sql);
	    if ($stmt == FALSE) 
	    {
	        throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
	    }

	    $result = $stmt->execute();
	    if ($result == FALSE) 
	    {
	        throw new Exception("execute of [$sql] failed " . $stmt->error);
	    }

	    $mysqli->close();
	}

	/**
    *@param int
    *@return array() Exerciselist
    **/
	public function getAllWorkoutExercisesList($workoutID)
	{	
		  $sql = " SELECT e.ExerciseID, e.MuscleGroupID, e.Name, e.Description, e.YoutubeLink, e.UserInfoID, we.Rep, we.Sets, we.Order
  				FROM workoutexercises AS we
  				INNER JOIN exercise AS e ON we.ExerciseID = e.ExerciseID
  				WHERE we.WorkoutID =$workoutID
  				ORDER BY e.MuscleGroupID";

	    return $this->WorkoutExerciseList($sql);
	}

	/**
    *@param string sql
    *@throws new exceptions
    *@return array exercises
    **/
    public function WorkoutExerciseList($sql)
    {
        $mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());


        $stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE) 
        {
            throw new Exception($sql);
        }

        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $result = $stmt->bind_result($exerciseID, $muscleGroupID, $name, $description, $youtubeLink,$userID , $rep, $set, $order);
        if ($result == FALSE)
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $exerciseList = array();

        while ($stmt->fetch()) 
        {
            $musclegroupName = $this->exerciseDAL->getMuscleGroupNameOnID($muscleGroupID);
            
            $exerciseList[] = Exercise::createWorkoutExerciseFromDatabase($musclegroupName,
                                                                          $muscleGroupID,
                                                                          $name, 
                                                                          $description,
                                                                          $youtubeLink,
                                                                          $userID, 
                                                                          $exerciseID, 
                                                                          $set, $rep, $order);
        }
        $mysqli->close();
        return $exerciseList;
    }
	



	/**
    *@param int
    *@param Exercise
    *@param int array
    *@param int array
    *@param int array
    *@throws new exceptions
    **/
	public function saveNewWorkoutExercise($workoutID, $ex, $reps, $sets, $order)  
	{
		$mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

		$sql = "INSERT INTO `workoutexercises`(`WorkoutID`, 
                                               `ExerciseID`, 
                                               `Rep`, 
                                               `Sets`, 
                                               `Order`)
                                                VALUES(?,?,?,?,?)";

        $statement = $mysqli->prepare($sql);
        if ($statement === FALSE) 
        {
                 throw new Exception("prepare of $sql failed " . $mysqli->error);
        }

        $ex = (int)$ex;  
        if ($statement->bind_param("iiiii", $workoutID,$ex,$reps, $sets, $order) === FALSE) 
        {
                throw new Exception("bind_param of $sql failed " . $statement->error);
        }
        if ($statement->execute() === FALSE) 
        {
                throw new Exception("execute of $sql failed " . $statement->error);
        }
		$mysqli->close();

	}
}